import time, threading, os

v_data = []
a_data = []
s_data = []

class FileReader(threading.Thread):

    def __init__(self, video, screen, window):
        threading.Thread.__init__(self)
        self.video = video
        self.screen = screen
        self.window = window

    def run(self):
        global v_data, f

        idx = 2048    # movi block
        vfileSize = os.path.getsize(self.video)
        vf = open(self.video,"rb")
        text = vf.read(vfileSize + 100)

        while (True):
            if text[idx:idx+4] == "00dc":
                frameSize = 16 ** 2 * ord(text[idx+5]) + ord(text[idx+4])
                if frameSize > 0:
                    v_data.append(text[idx+12:idx+frameSize+8])
                else:        # empty frame
                    v_data.append("")
                idx += frameSize + 8
            elif text[idx:idx+4] == "00db":    # key frame
                frameSize = 16 ** 2 * ord(text[idx+5]) + ord(text[idx+4])
#                v_data.append(text[idx+12:idx+34])
#                v_data.append(text[idx+38:idx+43])
                v_data.append(text[idx+12:idx+frameSize+8])
                idx += frameSize + 8
            else:
                if text[idx] == '\x00':
                    idx += 1
                else:
                    break

        vf.close()

        idx = 2048
        safileSize = os.path.getsize(self.screen)
        saf = open(self.screen,"rb")
        satext = saf.read(safileSize + 100)

        while (True):
            if satext[idx:idx+4] == "01wb":        # audio frame
                frameSize = 16 ** 2 * ord(satext[idx+5]) + ord(satext[idx+4])
                if frameSize > 0:
                    a_data.append(satext[idx+8:idx+frameSize+8])
                else:
                    a_data.append("")
                idx += frameSize + 8
            elif satext[idx:idx+4] == "00dc":       # screen frame
                frameSize = 16 ** 2 * ord(satext[idx+5]) + ord(satext[idx+4])
                s_data.append(satext[idx+8:idx+frameSize+8])
                idx += frameSize + 8
            else:
                if satext[idx] == '\x00':
                    idx += 1
                else:
                    break

        saf.close()

        print len(v_data)
        print len(a_data)
        print len(s_data)

    def stop(self):
        print "stop"